/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plant.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author JAVA工程师---李琦
 * 			----上传图接口 
 * 
 */
@Controller
@RequestMapping(value = "/photo")
public class UploadAction {
    @RequestMapping(value = "/upload")  
    public String upload(@RequestParam(value = "file", required = false) MultipartFile[] myfiles, HttpServletRequest request, ModelMap model) throws IOException, FileUploadException {  
        List<Object> list=new ArrayList<Object>();
        String photoName=null;
        System.out.println("开始上传");  
        //如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解  
        //如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解  
        //并且上传多个文件时，前台表单中的所有<input type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件
        for(MultipartFile myfile : myfiles){  
            if(myfile.isEmpty()){  
                System.out.println("文件未上传");
                return null;
            }else{  
                System.out.println("文件长度: " + myfile.getSize());  
                System.out.println("文件类型: " + myfile.getContentType());  
                System.out.println("文件名称: " + myfile.getName());  
                System.out.println("文件原名: " + myfile.getOriginalFilename());  
                System.out.println("========================================");  
                String fileName=myfile.getOriginalFilename();
                fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
                System.out.println("文件名称: " + fileName);  
                //获取扩展
                String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
                System.out.println("文件名称: " + extName);  
                 //UUID
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String newName = uuid+extName;   
                //如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中  
//                String realPath = request.getSession().getServletContext().getRealPath("/upload");  
                String realPath = request.getSession().getServletContext().getInitParameter("UPLOAD_IMAGE_PATH");//从配置文件中读取要保存的位置  
                //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的  
                org.apache.commons.io.FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,newName));  
                list.add(newName);
            }  
        }  
        for(int i=0;i<list.size();i++){
            photoName=(String) list.get(i);
            System.out.println("我是图片给名字"+photoName);
        }
         return photoName;  
    }  
    
}
